package cn.com.surker.production.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.oa.entity.OaProductionContract;
import cn.com.surker.production.entity.PumpConDevice;
import cn.com.surker.production.dao.PumpConDeviceDao;
import cn.com.surker.production.entity.PumpDevice;
import cn.com.surker.production.service.PumpConDeviceService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * <p>
 * 泵送合同关联设备表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-09-04
 */
public abstract class PumpConDeviceBaseImpl extends ServiceImpl<PumpConDeviceDao, PumpConDevice> implements PumpConDeviceService {

	@Resource
	protected PumpConDeviceDao pumpConDeviceDao;

	@Override
	public List<PumpConDevice> queryByConDev(Long conId, Long devId) {
		QueryWrapper<PumpConDevice> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		if (conId != null) {
			qw.eq("pro_con_id", conId);
		}
		if (devId != null) {
			qw.eq("pump_device_id", devId);
		}
		List<PumpConDevice> pumpConDevices = pumpConDeviceDao.selectList(qw);
		return pumpConDevices;
	}

	@Override
	public List<OaProductionContract> findDevConList(Long orgId, Long devId) {
		List<OaProductionContract> contracts = pumpConDeviceDao.findConList(orgId, devId);
		return contracts;
	}

	@Override
	public PumpConDevice findConByDevId(Long orgId, Long devId, String pumpTime) {
		if (orgId == null || devId == null || pumpTime == null) {
			return null;
		}
		QueryWrapper<PumpConDevice> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.eq("pump_device_id", devId);
		qw.le("start_time", pumpTime);
		qw.and(e -> {
			//	截止时间大于运输时间
			e.isNull("end_time").or().gt("end_time", pumpTime);
		});
		qw.last("limit 1");
		PumpConDevice conDevice = pumpConDeviceDao.selectOne(qw);
		return conDevice;
	}

	/**
	 * 分页查询泵车关联信息
	 * @param searchVal
	 * @param contractId
	 * @param status
	 * @param offset
	 * @param limit
	 * @return
	 */
	public IPage findPage(String searchVal, Long contractId, Integer status, Integer offset, Integer limit) {
		PageHelper.offsetPage(offset, limit);
		if (StringUtils.isNull(status))
			status = 0;
		if (StringUtils.isNull(searchVal))
			searchVal = null;
		IPage iPage = pumpConDeviceDao.findPageVo(new Page<>((offset + limit) /limit, limit),
				searchVal, contractId, status);
		/**分页插件**/
		if (!StringUtils.isNull(iPage.getRecords())) {
			PageInfo pageInfo = new PageInfo(iPage.getRecords());
			if (!StringUtils.isNull(pageInfo.getTotal()))
				iPage.setTotal(pageInfo.getTotal());
		}
		return iPage;
	}

	@Override
	protected Class<PumpConDevice> currentModelClass() {
		return PumpConDevice.class;
	}
}